<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义指令</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }

        ul {
            width: 1000px;
            margin: 0 auto;
            list-style: none;
            display: flex;
            justify-content: space-evenly;
        }

        li {
            width: 300px;
            height: 200px;
        }

        img {
            width: 100%;
            height: 100%;
        }
    </style>



</head>

<body>
    <div id="app">
        <ul>
            <li v-for="item in picList" :Key="item" v-img>
                <img :src="item" alt="">
            </li>
        </ul>
    </div>


    <!-- 引入vue -->
    <script src=" https://unpkg.com/vue@3/dist/vue.global.js">
    </script>
    <script>
        const { createApp, ref, reactive, toRefs, computed, getCurrentInstance } = Vue;
        // 创建Vue实例
        const app = createApp({
            setup() {
                const state = reactive({
                    picList: [
                        'https://picsum.photos/id/237/300/200',
                        'https://picsum.photos/id/28/300/200',
                        'https://picsum.photos/id/29/300/200'
                    ]
                })
                return {
                    ...toRefs(state)
                }
            }

        })
        //获取随机颜色
        function getRandomColor() {
            var letters = '0123456789ABCDEF';
            var color = '#';
            for (var i = 0; i < 6; i++) {
                color += letters[Math.floor(Math.random() * 16)];
            }
            return color;
        }



        // 自定义指令
        // app.directive("img", {
        //     mounted(el, binding) {
        //         el.style.backgroundColor = getRandomColor()
        //     }
        // })
        app.directive("img", (el, binding) => {
            el.style.backgroundColor = getRandomColor()
        })


        // 挂载到dom
        app.mount('#app')
    </script>
</body>

</html>